package cn.weichuyu.snippet.repayplan.util;

import java.time.LocalDate;

public class MyDateUtil {
    public static LocalDate parseStr2LocalDate(String str) {
        if (str == null || (str.length() != 8 && str.length() != 10)) {
            throw new RuntimeException("日期格式不支持");
        }
        if (str.length() == 10) {
            return LocalDate.parse(str);
        }
        if (str.length() == 8) {
            String year = str.substring(0, 4);
            String month = str.substring(4, 6);
            String day = str.substring(6, 8);
            String newStr = year + "-" + month + "-" + day;
            return LocalDate.parse(newStr);
        }
        return null;
    }

    public static LocalDate parseRepayDay(LocalDate currentMonth, int repayDay) {
        return LocalDate.of(currentMonth.getYear(), currentMonth.getMonth(), repayDay);
    }

    public static LocalDate parseRepayDay(LocalDate currentMonth, int repayDay, int plusMonths) {
        LocalDate nextMonths = currentMonth.plusMonths(plusMonths);
        return LocalDate.of(nextMonths.getYear(), nextMonths.getMonth(), repayDay);
    }
}
